/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model.persistence;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.Config;
import br.com.jr2tecnologia.model.repository.ConfigRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;

/**
 *
 * @author ronildo
 */
public class ConfigDAO extends GenericDAO<Config, ConfigRepository> implements ConfigRepository {

    private static final Logger logger = LoggerFactory.getLogger();

    public Config findById(Long id) {
        return (Config)em.createNamedQuery("Config.findByPkConfig").
                setParameter("pkConfig", id).getSingleResult();
    }

    public Config findByName(String name) {
        try{
            return (Config)em.createNamedQuery("Config.findByName").
                setParameter("name", name).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.WARNING, "Cannot find data");
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public List<Config> list(Config config){
        String sql = "SELECT c FROM Config c";
        return em.createQuery(sql).getResultList();
    }

    public List<Config> listAll(){
        try{
            return em.createNamedQuery("Config.findAll").getResultList();
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

}
